﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mindfor.Web.Config;
using System.Text.RegularExpressions;
using Mindfor.Web.Models;

namespace Mindfor.Web.Data
{
	/// <summary>
	/// Provides extensions functions to query users.
	/// </summary>
	public static class QueryUserExtensions
	{
		/// <summary>
		/// Returns user by login.
		/// </summary>
		/// <param name="login">User login.</param>
		/// <returns>User or <c>Null</c> if not found.</returns>
		public static User GetUser(this IQueryable<User> users, string login)
		{
			if (login == null)
				return null;
			return users.FirstOrDefault(u => u.Login.ToLower() == login.ToLower());
		}

		/// <summary>
		/// Returns user by e-mail.
		/// </summary>
		/// <param name="email">User e-mail.</param>
		/// <returns>User or <c>Null</c> if not found.</returns>
		public static User GetUserByEmail(this IQueryable<User> users, string email)
		{
			if (email == null)
				return null;
			return users.FirstOrDefault(u => u.Email.ToLower() == email.ToLower());
		}

		/// <summary>
		/// Determines whether user with such login.
		/// </summary>
		/// <param name="login">User login.</param>
		public static bool CheckLoginExist(this IQueryable<User> users, string login)
		{
			if (String.IsNullOrEmpty(login))
				throw new ArgumentNullException("login");
			return users.GetUser(login) != null;
		}

		/// <summary>
		/// Determines whether user with such e-mail exists.
		/// </summary>
		/// <param name="email">User e-mail.</param>
		public static bool CheckEmailExist(this IQueryable<User> users, string email)
		{
			if (String.IsNullOrEmpty(email))
				throw new ArgumentNullException("email");
			return users.GetUserByEmail(email) != null;
		}
	}
}
